# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2022/11/4 16:34
desc:
'''
import time

from appium.webdriver.common.appiumby import AppiumBy

from proj_spec.CLUE.mobile_po.clue_app_base_page import CLUEAppBasePage


class SurroundRegionSelectionPage(CLUEAppBasePage):
    _reset_region_locator = (AppiumBy.XPATH, "//*[contains(@text,'重置')]")
    _confirm_region_locator = (AppiumBy.XPATH, "//*[contains(@text,'确定')]")

    def choose_distance(self, distance):
        """选择距离

        :param distance: 距离选项的文本

        :return:
        """
        self._distance_locator = (AppiumBy.XPATH, "//*[contains(@text,'%s')]" % distance)
        self.find_element_and_click(self._distance_locator)

    def choose_district(self, district):
        """当前第二级行政区仅宁夏回族自治区、青海省、中国台湾省不可滑动，第三级行政区在数量少的情况下不可滑动。第一级行政区为页面上第4个可滚动元素

        :param district:

        :return:
        """
        #page_source = self.driver.page_source
        district_trail = district.split('/')
        unscrollable_count = 0 #记录行政区不可滚动的计数
        # 需打开不可见元素
        #self.driver.update_settings({"allowInvisibleElements": True})
        time.sleep(3)

        if district_trail[0]!='上海市': #
            self.find_element_and_click((AppiumBy.XPATH, "//*[contains(@text,'1KM')]"))

        for i,text in enumerate(district_trail):
            try:
                # self.find_element_and_click((AppiumBy.ANDROID_UIAUTOMATOR,
                #                              'new UiScrollable(new UiSelector().scrollable(true).instance(%s))'%(i+3-unscrollable_count)))
                self.find_element_and_click((AppiumBy.ANDROID_UIAUTOMATOR,
                                             'new UiScrollable(new UiSelector().scrollable(true).instance(%s)).scrollIntoView(new UiSelector().textContains("%s"));'
                                             %(i+3-unscrollable_count,text)))
                # self.scroll_into_view((AppiumBy.ANDROID_UIAUTOMATOR,
                #                              'new UiScrollable(new UiSelector().scrollable(true).instance(%s))'%(i+3-unscrollable_count)))
                #self.find_element_and_click((AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("%s");' % text))
            except Exception as e: #unscrollable
                unscrollable_count+=1
                self.find_element_and_click((AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("%s")'%text))
            time.sleep(2)
        #self.driver.update_settings({"allowInvisibleElements": False})


    def choose_region(self,**kwargs):
        """

        :param kwargs:

        :return:
        """
        #self.find_element_and_click((AppiumBy.XPATH, "//*[contains(@text,'重置')]"))
        if 'distance' in kwargs:
            self.choose_distance(kwargs['distance'])

        if 'district' in kwargs:
            self.choose_district(kwargs['district'])

        #test = self.find_element(self._confirm_region_locator)
        self.find_element_and_click(self._confirm_region_locator)

    def get_button_rect(self, text):
        """ 获取带有text文本的按钮的rect对象

        :param text: 按钮包含的文本

        :return: rect对象（字典） 键值包括x,y,height,width
        """
        # button_locator = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("%s")'%text)
        # button = self.find_element(button_locator)
        return self.get_element_rect((AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("%s")'%text))

